﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="Ornament.Messages" %>
<%@ Import Namespace="Ornament.Web" %>
<%@ Import Namespace="Ornament.MemberShip.Permissions" %>
<link href="/Scripts/treeView/jquery.treeview.css" rel="stylesheet" type="text/css" />

<script type="text/C#" runat="server">
    //Key is the InfoType's Id, Value is the Resource's Id
    Dictionary<Guid, Guid> listId = new Dictionary<Guid, Guid>();
    private void buldListId()
    {

        var infosTypeRes = OrnamentContext.Current.MemberShipFactory.CreateResourceDao()
               .GetResourceByType(typeof(GenericResourceInfo<InfoType>));

        foreach (GenericResourceInfo<InfoType> res in infosTypeRes)
        {
            listId.Add(res.Value.Id, res.Id);
        }
    }

    private Guid BuildResource(InfoType type, Ornament.MemberShip.IResourceDao dao)
    {
        if (listId.Count == 0)
        {
            buldListId();
        }

        foreach (var id in listId.Keys)
        {
            if (type.Id == id)
                return listId[id];
        }
        GenericResourceInfo<InfoType> res = new GenericResourceInfo<InfoType>(type, typeof(InfoOperator));
        res.Name = type.Name;
        dao.Save(res);
        return res.Id;
    }

    private void Build(IList<InfoType> childNodes)
    {
        var dao = OrnamentContext.Current.MemberShipFactory.CreateResourceDao();

        foreach (var node in childNodes)
        {
            var resId = BuildResource(node, dao);
            if (node.Count == 0)
            {
                Response.Write(String.Format(("<li><a id='{0}' title='{2}'>{1}</a></li>"), resId.ToString(), node.Name, node.Comment));
            }
            else
            {
                Response.Write(String.Format(("<li><a id='{0}' title='{2}'>{1}</a>"), resId.ToString(), node.Name, node.Comment));
                Response.Write("<ul>");
                this.Build(node.GetChilds());
                Response.Write("</ul>");
                Response.Write("</li>");
            }
        }
    }

    IList<InfoType> infoTypes = OrnamentContext.Current.MessageManager.Factory.InfoTypeDao.GetFirstLevel();
</script>

<script src="/Scripts/treeView/jquery.treeview.js" type="text/javascript"></script>

<input type="hidden" name="resourceId" id="resourceId" />
<div id="infoTypeSelectorPanel">
    <ul id="infoTypeTree">
        <li><a href="#<%=Guid.Empty.ToString()%>">无父类型</a>
            <ul>
                <%
                    Build(infoTypes);
                %>
            </ul>
        </li>
    </ul>
</div>

<script type="text/javascript">
    $("a", $("#infoTypeTree").treeview()).click(function() {
        var code = $("#resourceId").val();
        if (code != "") {
            $("#" + code).removeClass("ui-state-default");
        }
        var id = $(this).addClass("ui-state-default").attr("id");

        $("#resourceId").val(id);
    });
</script>

